﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Top.Data;
using Top.Web;
using Top.Web.Api;

namespace Top.PointStore.Business
{
    public static class GlobalResourceManager
    {
        //自旋锁控制UserPoints并发操作
        private static System.Threading.SpinLock spinLock = new System.Threading.SpinLock();

        private static List<T_PointMain_new> userPoints;

        public static T_PointMain_new GetMyPoint(OAuthUser user)
        {
            try
            {
                T_PointMain_new myPointx = MemoryTable.TB_PointMain_new[user.UserToken.ToString().ToUpper()];
                return myPointx;
            }
            catch
            {
                T_PointMain_new point = PointManager.GetV2(user).Result;
                MemoryTable.TB_PointMain_new.TryAdd(point.F_UserToken, point);
                return point;
            }
        }
        /// <summary>
        /// 修改内存积分
        /// </summary>
        /// <param name="user">当前用户</param>
        /// <param name="pointMain">修改后的积分对象</param>
        public static void UpdateMyPoint_Memory(OAuthUser user,T_PointMain_new pointMain)
        {
            T_PointMain_new myPoint = null;
            try
            {
                myPoint = MemoryTable.TB_PointMain_new[user.UserToken.ToString().ToUpper()];
            }
            catch
            {
                myPoint = PointManager.GetV2(user).Result;
                MemoryTable.TB_PointMain_new.TryAdd(myPoint.F_UserToken.ToUpper(), myPoint);
            }
            if (myPoint == null)
            {
                return;
            }
            bool getTimeLock = false;
            try
            {
                spinLock.Enter(ref getTimeLock);
                MemoryTable.TB_PointMain_new[user.UserToken.ToString().ToUpper()] = pointMain;
            }
            finally
            {
                if (getTimeLock) spinLock.Exit();
            }
        }
        public static void UpdateMyPoint_Memory(OAuthUser user)
        {
            UpdateMyPoint_Memory(user, PointManager.GetV2(user).Result);
        }
        public static T_OAuthInfo CheckUserTokenIsExist(OAuthUser user)
        {
            try
            {
                T_OAuthInfo myOAuth = MemoryTable.TB_OAuthInfo[user.UserToken.ToString().ToUpper()];
                return myOAuth;
            }
            catch
            {
                bool isExist = OAuthInfoManager.GetUserToken(user.UserToken.ToString()).Result;
                if (isExist)
                {
                    //数据库已存在，但内存中不存在 补充内存
                    MemoryTable.TB_OAuthInfo.TryAdd(user.UserToken.ToString().ToUpper(), new T_OAuthInfo()
                    {
                        F_UserToken = user.UserToken.ToString().ToUpper(),
                        F_AdminKid = user.CrmUserID,
                        F_BrokerKid = user.BrokerID
                    });
                }
                else
                {
                    //数据库中不存在，补充至数据库中
                    MemoryTable.TB_OAuthInfo.TryAdd(user.UserToken.ToString().ToUpper(), new T_OAuthInfo()
                    {
                        F_UserToken = user.UserToken.ToString().ToUpper(),
                        F_AdminKid = user.CrmUserID,
                        F_BrokerKid = user.BrokerID
                    });
                    OAuthInfoManager.AddOAuthInfo(user.UserToken, user).Wait();
                }
                T_OAuthInfo myOAuth = MemoryTable.TB_OAuthInfo[user.UserToken.ToString().ToUpper()];
                return myOAuth;
            }
        }

        public static T_PointMain_new CheckPointMainIsExist(OAuthUser user)
        {
            try
            {
                T_PointMain_new myPointx = MemoryTable.TB_PointMain_new[user.UserToken.ToString().ToUpper()];
                return myPointx;
            }
            catch
            {
                T_PointMain_new point = PointManager.GetV2(user).Result;
                MemoryTable.TB_PointMain_new.TryAdd(point.F_UserToken, point);
                return point;
            }
        }

        public static void CheckBrokerOrAdimIsExist(OAuthUser user)
        {
            if (user.BrokerID > 0)
            {
                if (!MemoryTable.TB_Broker.Keys.Contains(user.BrokerID))
                {
                    DataBaseSyncManager.Broker_Sync(user).Wait();
                }
            }
            if (user.CrmUserID > 0)
            {
                if (!MemoryTable.TB_Admin.Keys.Contains(user.CrmUserID))
                {
                    DataBaseSyncManager.Admin_Sync(user).Wait();
                }
            }
        }

        public async static Task<bool> AuthCheck(int brokerKid)
        {
            string sql = @" 
             select b.F_CityKid from t_broker a left join t_brokercompany b on a.F_BrokerCompanykid = b.kid
             where a.kid = {0} and b.kid is not null
             limit 1";
            try
            {
                using (var db = DataBaseContext.GetDataBase("reader"))
                {
                    int city = await db.Sql(sql, brokerKid).ExecuteScalar<int>();
                    if (city == 112||city==115)
                    {
                        return true;
                    }
                    return false;
                }
            }
            catch
            {
                return false;
            }
            
        }
    }
}
